package com.briup.estore.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * mybatis的基本封装
 */
public class MyBatisSqlSessionFactory {
    private static SqlSessionFactory sqlSessionFactory;

    /**
     * 默认的sqlSessionFactory
     * @return
     * @throws IOException
     */
    public static SqlSessionFactory getSqlSessionFactory() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        return new SqlSessionFactoryBuilder().build(is);
    }

    /**
     * 根据Environment进行创建
     * @param environment 环境
     * @return
     * @throws IOException
     */
    public static SqlSessionFactory getSqlSessionFactory(String environment) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        return new SqlSessionFactoryBuilder().build(is,environment);
    }
        public static SqlSession openSession(boolean autoCommit) throws IOException {
        return getSqlSessionFactory().openSession(autoCommit);
    }
    public static SqlSession openSession() throws IOException {
        return openSession(true);
    }


}


